<template>
  <div class="reg-container">
    <div class="reg-box">
      <h2>注册</h2>
      <form @submit.prevent="handleRegister" class="reg-form">
        <div class="form-group">
          <input
            type="text"
            v-model="name"
            placeholder="用户名"
            required
            class="form-input"
          />
        </div>
        <div class="form-group">
          <input
            type="password"
            v-model="password"
            placeholder="密码"
            required
            class="form-input"
          />
        </div>
        <div class="form-group">
          <input
            type="password"
            v-model="confirmPassword"
            placeholder="确认密码"
            required
            class="form-input"
          />
        </div>
        <button type="submit" class="reg-button" :disabled="isLoading">
          {{ isLoading ? '注册中...' : '注册' }}
        </button>
        <button type="button" class="register-button" @click="goToRegister">
          登录
        </button>
        <p v-if="error" class="error-message">{{ error }}</p>
        <p v-if="successMessage" class="success-message">{{ successMessage }}</p>
      </form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const name = ref('')
const password = ref('')
const confirmPassword = ref('')
const error = ref('')
const isLoading = ref(false)
const countdown = ref(0)
const successMessage = ref('')

const goToRegister = () => {
  router.push('/login')
}
const handleRegister = async () => {
  try {
    error.value = ''
    successMessage.value = ''
    isLoading.value = true

    // 验证密码是否匹配
    if (password.value !== confirmPassword.value) {
      error.value = '两次输入的密码不一致'
      return
    }

    console.log('开始注册流程，尝试调用注册接口...')

    // 使用正确的接口地址
    const response = await fetch('http://localhost:3005/auth/register', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        name: name.value,
        password: password.value
      })
    })

    const data = await response.json()
    console.log('注册API返回数据:', data)

    if (response.ok && data.success === '成功') {
      // 注册成功，显示倒计时
      console.log('注册成功，准备倒计时跳转到登录页面')
      successMessage.value = '注册成功！3秒后跳转到登录页面...'
      
      // 开始3秒倒计时
      countdown.value = 3
      const timer = setInterval(() => {
        countdown.value -= 1
        successMessage.value = `注册成功！${countdown.value}秒后跳转到登录页面...`
        
        if (countdown.value <= 0) {
          clearInterval(timer)
          router.push('/login')
        }
      }, 1000)
    } else {
      console.error('注册失败:', data)
      error.value = data.message || '注册失败，请稍后重试'
    }
  } catch (e) {
    console.error('注册请求异常:', e)
    error.value = '注册失败，请检查网络连接或服务器状态'
  } finally {
    isLoading.value = false
  }
}
</script>

<style scoped>
.reg-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  width: 100%;
  margin: 0;
  padding: 0;
  background: linear-gradient(135deg, #42b883 0%, #35495e 100%);
  animation: gradientAnimation 10s ease infinite;
  background-size: 200% 200%;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}
.register-button {
  background: transparent;
  color: #42b883;
  padding: 15px;
  border: 2px solid #42b883;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  text-transform: uppercase;
  letter-spacing: 1px;
}

@keyframes gradientAnimation {
  0% { background-position: 0% 50% }
  50% { background-position: 100% 50% }
  100% { background-position: 0% 50% }
}

.reg-box {
  width: 90%;
  max-width: 380px;
  padding: 35px 25px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 20px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  transform: translateY(0);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.reg-box:hover {
  transform: translateY(-5px);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.15);
}

h2 {
  text-align: center;
  color: #35495e;
  margin-bottom: 35px;
  font-size: 28px;
  font-weight: 600;
  letter-spacing: 1px;
}

.reg-form {
  display: flex;
  flex-direction: column;
  gap: 25px;
}

.form-group {
  position: relative;
}

.form-input {
  width: 100%;
  padding: 15px;
  border: 2px solid #e8e8e8;
  border-radius: 12px;
  font-size: 16px;
  transition: all 0.3s ease;
  background: white;
  box-sizing: border-box;
  color: #000;
}

.form-input:focus {
  border-color: #42b883;
  outline: none;
  box-shadow: 0 0 0 3px rgba(66, 184, 131, 0.2);
}

.reg-button {
  background: linear-gradient(45deg, #42b883, #3aa876);
  color: white;
  padding: 15px;
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  text-transform: uppercase;
  letter-spacing: 1px;
}

.reg-button:hover {
  background: linear-gradient(45deg, #3aa876, #42b883);
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(66, 184, 131, 0.3);
}

.reg-button:disabled {
  background: linear-gradient(45deg, #9be0c0, #b8e9d3);
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.error-message {
  color: #ff6b6b;
  text-align: center;
  margin: 0;
  font-size: 14px;
  padding: 8px;
  border-radius: 8px;
  background: rgba(255, 107, 107, 0.1);
  animation: shake 0.5s ease-in-out;
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-5px); }
  75% { transform: translateX(5px); }
}

.success-message {
  color: #42b883;
  text-align: center;
  margin: 0;
  font-size: 14px;
  padding: 8px;
  border-radius: 8px;
  background: rgba(66, 184, 131, 0.1);
  animation: pulse 1.5s infinite;
}

@keyframes pulse {
  0% { opacity: 0.8; }
  50% { opacity: 1; }
  100% { opacity: 0.8; }
}
</style>